import router from './router'
import store from './store'

router.beforeEach(async (to, from, next) => {
  const whiteList = ['/login']
  const { token } = store.state.user
  // 如果有token
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      const userInfo = store.state.user.userInfo
      if (userInfo) {
        await store.dispatch('user/getUserInfoAction')
      }
      next()
    }
  } else { // 没有token
    // 判断是否在白名单种
    if (whiteList.includes(to.path)) {
      next()
    } else { // 不在白名单中， 跳转登录，并携带回调地址
      next(`/login?url=${to.path}`)
    }
  }
})
